package nckd.wnqjt.tmc.tda.plugin.form;

import java.util.Arrays;
import java.util.EventObject;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.PackageDataEvent;
import kd.bos.entity.report.ReportColumn;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.report.ReportShowParameter;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @author husheng
 * @date 2024-11-04 20:08
 * @description 承兑汇票
 */
public class AcceptancebillFormDataPlugin extends AbstractReportFormPlugin implements BeforeF7SelectListener {
    @Override
    public void initDefaultQueryParam(ReportQueryParam queryParam) {
        super.initDefaultQueryParam(queryParam);
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);

        this.addF7Listener(this, "nckd_company");
    }

    private void addF7Listener(BeforeF7SelectListener form, String... f7Names) {
        BasedataEdit f7 = null;
        String[] var4 = f7Names;
        int var5 = f7Names.length;

        for (int var6 = 0; var6 < var5; ++var6) {
            String f7Name = var4[var6];
            f7 = this.getControl(f7Name);
            if (f7 != null) {
                f7.addBeforeF7SelectListener(form);
            }
        }

    }

    @Override
    public void beforeQuery(ReportQueryParam queryParam) {
        super.beforeQuery(queryParam);
    }

    @Override
    public boolean verifyQuery(ReportQueryParam queryParam) {
        // 轻分析穿透
        ReportShowParameter parameter = (ReportShowParameter) this.getView().getFormShowParameter();
        if (parameter.getReportQueryParam() != null) {
            Map<String, Object> customParams = parameter.getReportQueryParam().getCustomParam();
            if (ObjectUtils.isNotEmpty(customParams)) {
                queryParam.setCustomParam(customParams);
                return true;
            }
        }

        // 必填校验
//        IDataModel model = this.getModel();
//        DynamicObject dataEntity = this.getModel().getDataEntity(true);
//        DynamicObjectCollection company = dataEntity.getDynamicObjectCollection("nckd_company");
//        if (company == null || company.size() == 0) {
//            this.getView().showTipNotification(ResManager.loadKDString("请检查必录项", "StockGatherRptFormPlugin_0", "fi-cal-report", new Object[0]));
//            return false;
//        }

        return super.verifyQuery(queryParam);
    }

    @Override
    public void setMergeColums(List<String> columns) {
        super.setMergeColums(columns);

        columns.add("nckd_drawercompanyname");
        columns.add("nckd_drawerbankname");
        columns.add("nckd_t_totalamt");
    }

    @Override
    public void packageData(PackageDataEvent packageDataEvent) {
        super.packageData(packageDataEvent);

        ReportColumn c = (ReportColumn) packageDataEvent.getSource();
        //当前读取的列
        String fieldKey = c.getFieldKey();

        if ("nckd_drawerbankname".equals(fieldKey) && "小计".equals(packageDataEvent.getFormatValue().toString())) {
            packageDataEvent.getNoMergeKey().add(fieldKey);
        }
        if ("nckd_drawerbankname".equals(fieldKey)) {
            //某列某行单元格的数据
            String s = packageDataEvent.getFormatValue().toString();
            //获取上一行数据
            String condition_prevalue = this.getPageCache().get("srstatusa_prevalue");
            if (condition_prevalue != null && !condition_prevalue.isEmpty() && !condition_prevalue.equals(s)) {
                //设置取消某列当前行与上一行的合并
                packageDataEvent.getNoMergeKey().add("nckd_t_totalamt");
            }
            //把当前所在单元格数据保存到页面缓存中
            this.getPageCache().put("srstatusa_prevalue", s);
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
        QFilter qFilter = new QFilter("user", QCP.equals, UserServiceHelper.getCurrentUserId())
                .and("role.number", QCP.equals, "qfx02");
        // 用户和通用角色关系
        DynamicObject[] dynamicObjects = BusinessDataServiceHelper.load("perm_userrole", "org", qFilter.toArray());
        List<Long> orgIdList = Arrays.stream(dynamicObjects).map(t -> t.getDynamicObject("org").getLong("id")).collect(Collectors.toList());

//        // 根据用户查询用户组
//        QFilter qFilter = new QFilter("user", QCP.equals, UserServiceHelper.getCurrentUserId());
//        // 用户组职员关系
//        DynamicObject[] bosUsergroupstaffs = BusinessDataServiceHelper.load("bos_usergroupstaff", "usergroup", qFilter.toArray());
//        List<Long> usergroup = Arrays.stream(bosUsergroupstaffs).map(t -> t.getDynamicObject("usergroup").getLong("id")).collect(Collectors.toList());
//
//        // 根据用户组查询组织(权限控制对象)
//        QFilter qFilter2 = new QFilter("usrgrpid", QCP.in, usergroup)
//                .and("role.number", QCP.equals, "qfx02");
//        // 用户组角色关系-多类别
//        DynamicObject[] permUsrgrproles = BusinessDataServiceHelper.load("perm_usrgrprole", "dimid", qFilter2.toArray());
//        List<Long> orgIdList = Arrays.stream(permUsrgrproles).map(t -> t.getDynamicObject("dimid").getLong("id")).collect(Collectors.toList());

        // 权限过滤
        beforeF7SelectEvent.addCustomQFilter(new QFilter("id", QCP.in, orgIdList));
    }
}
